using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.IO;
using System.IO.IsolatedStorage;
namespace MicroMsg.Scene
{
	public class NetSceneDownloadHDHeadImg : NetSceneBaseEx<GetHDHeadImgRequest, GetHDHeadImgResponse, GetHDHeadImgRequest.Builder>
	{
		private const string TAG = "NetSceneDownloadHDHeadImg";
		private const int MM_HD_HEADIMG_MAX_DOWNLOAD_SIZE = 480;
		private const string HD_FILE_FORMAT = "jpg";
		public const int MM_HD_HEADIMG_WEIXIN = 1;
		public const int MM_HD_HEADIMG_BOTTLE = 2;
		private uint mOffset;
		private uint mTotalLen;
		private int mTimesToServer;
		private string strUserName = "";
		private int mHeadImgType;
		private MemoryStream imgDownloadStream;
		private string bigHeadImgPathName;
		public bool doScene(string userName, int headImgType)
		{
			return new NetSceneDownloadHDHeadImg().doSceneEx(userName, headImgType);
		}
		private bool doSceneEx(string userName, int headImgType)
		{
			if (!string.IsNullOrEmpty(userName) && headImgType != 0)
			{
				this.mHeadImgType = headImgType;
				if (this.mHeadImgType == 2 && userName == AccountMgr.getCurAccount().strUsrName)
				{
					this.strUserName = AccountMgr.getCurAccount().strUsrName + ConstantValue.TAG_BOTTLE;
				}
				else
				{
					this.strUserName = userName;
				}
				return this.doScene();
			}
			return false;
		}
		private bool doScene()
		{
			if (string.IsNullOrEmpty(this.strUserName))
			{
				return false;
			}
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.StartPos = this.mOffset;
			this.mBuilder.TotalLen = this.mTotalLen;
			this.mBuilder.UserName = this.strUserName;
			this.mBuilder.ImgHeight = 480u;
			this.mBuilder.ImgWidth = 480u;
			this.mBuilder.HeadImgType = (uint)this.mHeadImgType;
			this.mBuilder.ImgFormat = "jpg";
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/gethdheadimg";
			base.endBuilder();
			this.mTimesToServer++;
			return true;
		}
		protected override void onFailed(GetHDHeadImgRequest request, GetHDHeadImgResponse response)
		{
			Log.smethod_1("NetSceneDownloadHDHeadImg", "send GetHDHeadImgRequest failed");
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR, null, null);
		}
		protected override void onSuccess(GetHDHeadImgRequest request, GetHDHeadImgResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "send GetHDHeadImgRequest failed ret =" + ret);
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			if (response.TotalLen <= 0u)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "Download head image failed because of response.TotalLen <= 0");
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			uint arg_5F_0 = response.StartPos;
			if (response.StartPos > response.TotalLen)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "Download head image failed because of server param error!");
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			if (this.mTimesToServer >= 10)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "Reach the max times to server!");
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			this.mTotalLen = response.TotalLen;
			if (this.imgDownloadStream == null)
			{
				this.imgDownloadStream = new MemoryStream();
			}
			int num = response.Data.Buffer.ToByteArray().Length;
			if (num <= 0)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "Get Image head lenght is 0");
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			this.writeBufIntoStream(response.Data.Buffer.ToByteArray(), this.mOffset, (uint)num, this.imgDownloadStream);
			this.mOffset += (uint)num;
			if (!this.hasDownloadComplete())
			{
				this.doScene();
				return;
			}
			IsolatedStorageFileStream isolatedStorageFileStream = this.openFile();
			if (isolatedStorageFileStream == null)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", "Create head image file failed");
				this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR);
				return;
			}
			this.imgDownloadStream.set_Position(0L);
			this.writeBufIntoStream(this.imgDownloadStream.ToArray(), 0u, (uint)this.imgDownloadStream.ToArray().Length, isolatedStorageFileStream);
			isolatedStorageFileStream.Flush();
			isolatedStorageFileStream.Close();
			this.doSceneFinished(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_SUCCESS);
		}
		private IsolatedStorageFileStream openFile()
		{
			if (string.IsNullOrEmpty(this.strUserName))
			{
				return null;
			}
			IsolatedStorageFileStream result;
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(StorageIO.getHeadImgHDPath()))
					{
						userStoreForApplication.CreateDirectory(StorageIO.getHeadImgHDPath());
					}
					this.bigHeadImgPathName = StorageMgr.hdheadimg.getPath(this.strUserName);
					if (string.IsNullOrEmpty(this.bigHeadImgPathName))
					{
						result = null;
					}
					else
					{
						if (userStoreForApplication.FileExists(this.bigHeadImgPathName))
						{
							userStoreForApplication.DeleteFile(this.bigHeadImgPathName);
						}
						IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.CreateFile(this.bigHeadImgPathName);
						result = isolatedStorageFileStream;
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneDownloadHDHeadImg", ex.get_Message());
				result = null;
			}
			return result;
		}
		public bool hasDownloadComplete()
		{
			return this.mOffset >= this.mTotalLen && this.mTotalLen > 0u;
		}
		private void writeBufIntoStream(byte[] byte_0, uint startPos, uint ndataLen, Stream mStream)
		{
			if (byte_0 != null && mStream != null)
			{
				try
				{
					mStream.Seek((long)((ulong)startPos), 0);
					mStream.Write(byte_0, 0, (int)ndataLen);
				}
				catch (Exception ex)
				{
					Log.smethod_1("NetSceneDownloadHDHeadImg", ex.get_Message());
				}
				return;
			}
		}
		private void doSceneFinished(EventConst eventConst_0)
		{
			if (eventConst_0 == EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_SUCCESS)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_SUCCESS, null, null);
				if (this.imgDownloadStream != null)
				{
					this.imgDownloadStream.Close();
					return;
				}
			}
			else
			{
				if (eventConst_0 == EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR)
				{
					if (this.imgDownloadStream != null)
					{
						this.imgDownloadStream.Close();
					}
					EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOAD_HEADIMG_ERR, null, null);
					return;
				}
				if (this.imgDownloadStream != null)
				{
					this.imgDownloadStream.Close();
				}
			}
		}
	}
}
